package com.dinzeer.sjapadder.overlay;

import com.dinzeer.legendreliclib.lib.util.slashblade.SlashBladeUtil;
import com.dinzeer.sjapadder.Sjap_adder;
import com.dinzeer.sjapadder.se.ShadowOfRevenant;
import mods.flammpfeil.slashblade.capability.slashblade.ISlashBladeState;
import mods.flammpfeil.slashblade.item.ItemSlashBlade;
import mods.flammpfeil.slashblade.registry.SpecialEffectsRegistry;
import mods.flammpfeil.slashblade.registry.specialeffects.SpecialEffect;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.client.gui.overlay.ForgeGui;
import net.minecraftforge.client.gui.overlay.IGuiOverlay;

import java.util.List;

public class ShadowOfRevenantOverlay implements IGuiOverlay {

    public static final ShadowOfRevenantOverlay INSTANCE = new ShadowOfRevenantOverlay();
    private final Minecraft minecraft = Minecraft.getInstance();
    private static final ResourceLocation HUD = new ResourceLocation(Sjap_adder.MODID, "textures/gui/shadow_of_revenant_gui.png");

    private ShadowOfRevenantOverlay() {

    }

    @Override
    public void render(ForgeGui gui, GuiGraphics guiGraphics, float partialTick, int screenWidth, int screenHeight) {
        Player player = minecraft.player;
        if (player.isSpectator()) return;
        ItemStack stackInHand = player.getItemInHand(InteractionHand.MAIN_HAND);

        if (stackInHand.getItem() instanceof ItemSlashBlade && stackInHand.hasTag()) {
            ISlashBladeState state = SlashBladeUtil.getState(stackInHand);
            CompoundTag nbt = stackInHand.getTag();
            List<ResourceLocation> effects = state.getSpecialEffects();
            for (ResourceLocation effectId : effects) {
                SpecialEffect effect = SpecialEffectsRegistry.REGISTRY.get().getValue(effectId);
                if (effect instanceof ShadowOfRevenant && nbt.contains(ShadowOfRevenant.SHADOW_OF_REVENANT)) {
                    CompoundTag shadowOfRevenantTag = nbt.getCompound(ShadowOfRevenant.SHADOW_OF_REVENANT);
                    int xInit = screenWidth / 2 + 24;
                    int yInit = screenHeight / 2;

                    for (int i = 0; i < 3; i++) {
                        String currentKey = ShadowOfRevenant.REVENANT_KEYS[i];
                        if (shadowOfRevenantTag.contains(currentKey)) {
                            int currentStack = shadowOfRevenantTag.getInt(ShadowOfRevenant.REVENANT_KEYS[i]);
                            guiGraphics.blit(HUD, xInit + (i * 10), yInit,
                                    0, 0, 8, 16, 16, 16);

                                guiGraphics.blit(HUD, xInit + 4 + (i * 10), yInit + 13 - (currentStack * 2),
                                    8, 0, 2, (currentStack * 2), 16, 16);
                        }
                    }

                    break;
                }
            }
        }

    }
}
